/* 
 * File:   Music.cpp
 * Author: agu
 * 
 * Created on 31 de mayo de 2014, 2:01
 */

#include "Music.h"
#include "InitializationException.h"

Music::Music() {
    this->music = NULL;
    this->keys = SDL_GetKeyboardState(NULL);
}

void Music::load(const string path) {
    this->music = Mix_LoadMUS(path.c_str());
    if (this->music == NULL)
        throw InitializationException(Mix_GetError());
}

Music::Music(const Music& orig) {
    this->music = orig.music;
}

Music::~Music() {
    Mix_FreeMusic(this->music);
}

void Music::play() {
    if (Mix_PlayingMusic() == 0) {
        //Play the music
        Mix_PlayMusic(this->music, -1);
    }
}

void Music::pause() {
    //If the music is paused
    if (Mix_PausedMusic() == 1) {
        //Resume the music
        Mix_ResumeMusic();
    }//If the music is playing
    else {
        //Pause the music
        Mix_PauseMusic();
    }
}

void Music::stop() {
    Mix_HaltMusic();
}

void Music::update() {
    if (this->keys[SDL_SCANCODE_1]) {
        this->play();
    }
    if (this->keys[SDL_SCANCODE_2]) {
        this->pause();
    }
    if (this->keys[SDL_SCANCODE_3]) {
        this->stop();
    }
}